<?php
include_once 'common/tools.php';
//启用一个带有ob_gzip压缩机的工作台
ob_start('ob_gzip');

$id = @$_GET["id"];
$uId = @$_GET["uId"];
$page = $_GET["page"];
$path = $_GET["path"];
$self = $_GET["self"];

$config = require 'conf/config.ini.php';      //加载公用config.ini.php
$conn = mysql_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"]);
mysql_select_db($config["DB_NAME"], $conn);
mysql_query("SET NAMES utf8");

$sql = "select t.topicId id, t.topicTitle title, t.topicContent content, u.userId, u.id uId, u.userPic, u.userCity, u.userSex,IFNULL(u.userSign, '') userSign, ". 
		"IF(u.userBirthDay <> '0000-00-00', (YEAR(CURDATE())-YEAR(u.userBirthDay))-(RIGHT(CURDATE(),5)<RIGHT(u.userBirthDay,5)), '0') userAge,".
		"(select count(replayId) from live_topic where replayId = t.topicId) rCount, t.updateDate, ".
		"IFNULL((select fState from live_friend where ((fromUserId = " . $uId . " and toUserId = u.id) or (toUserId = " . $uId . " and fromUserId = u.id) or (fromUserId = u.id and toUserId = 0))),'-1') f ".
		"from live_topic t ,live_user u ".
		"where  ".
		"		u.id = t.userId ".
		"and t.replayId = 0  ";
if($self == "1"){
	$sql .= "and t.userId = '". $uId ."'";
}else{
	$sql .= "and (u.id  = '". $uId ."' or  u.id in ( ".
		"		select toUserId from live_friend ".
		"		where fromUserId = '".$uId."' AND fState = '1' ".
		"		union all ".
		"		select fromUserId from live_friend ".
		"		where (toUserId = '".$uId."' AND	fState = '1') ".
		"		or		toUserId	= '0' ".
		"	)) ";
}
if($path == "1"){
	$sql .= " and t.topicId < " . $id;	
}else{
	$sql .= " and t.topicId > " . $id;	
}
$sql .= " order by t.updateDate desc";
if($path != "-1"){
	$sql .= " limit " . $page * 20 . ", 20";
} 
$list = mysql_query($sql, $conn);
while ($obj = mysql_fetch_object($list)) {
	$objArray[] = $obj;
}
echo '{"sql":"' . $sql . '","results":' . json_encode($objArray) . '}';
//echo '{"results":' . json_encode($objArray) . '}';
//输出压缩成果
ob_end_flush();
?>